import { observable, action, toJS } from 'mobx';
import { RouteLayout, SelectPage, Browser, BrowserHrm } from 'weaver-mobile-page';
import moment from 'moment';
import * as API from '../../api/oneWeek';
import * as Api from '../../api/workplanViewList';
export default class OneWeekStore {
  @observable loadding = false;
  @observable init = false;
  @observable selectGroup = [];
  @observable selectedType = [];
  @observable selectValue = [];
  @observable oneWeekDatas = [];
  @observable showNight = false;
  @observable showNoon = false;
  @observable layout = new RouteLayout.Store({ route: '/workPlanViewList', id: 'select' });
  @observable layoutMyfollow = new RouteLayout.Store({ route: '/workPlanViewList/calendar', id: 'myfollow' });
  @observable detailLayout = new RouteLayout.Store({ route: '/workPlanViewList/calendar', id: 'detail' });
  @observable selectedDate = moment().format('YYYY-MM-DD');
  @observable groupId = '';
  @observable weekStartDate = '';
  @observable weekEndDate = '';
  @observable isStyleX = true;
  @observable status = {
    current: 'oneweek'
  };
  @observable selectPage = new SelectPage.Store({ route: '/workPlanViewList/select' });
  @observable rangeSelectPage = new SelectPage.Store({ route: '/workPlanViewList/select' });
  @observable typeSelectPage = new SelectPage.Store({ route: '/workPlanViewList/select' });
  @observable selectedTypeVal = [];
  @observable selectedTypeLabel = '';
  @observable selectGroupVal = [];
  @observable typeSelectPageVal = []; 
  @observable dept = [];
  @observable subComp = [];
  @observable browser = new Browser.Store({ route: '/workPlanViewList/select' });
  @observable browserHrm = new BrowserHrm.Store({ route: '/workPlanViewList/select' });
  @observable browserHrm2 = new BrowserHrm.Store({ route: '/workPlanViewList/select' });
  @observable hrmBrowserVal = [];
  @observable commonBrowserVal = [];
  @observable typeBrowserVal = [];
  @observable typeVal = '17';
  @observable workPlanType = []; // 类型
  @observable objType = ''; // 类型
  @observable objIds = []; // 所选类型的ID
  @observable weekRangeEndDate = moment().format('YYYY-MM-DD');
  @observable titleFold = 0;
  @observable ViewDatas = {
    conf: {},
    membersDatas: {},
    days: {},
    memberids: [],
  };

  @action 
  setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key]
    })
  }

  doinitUrl = (url) => {// 初始化URL
    // console.log('url:', url)
    this.layout = new RouteLayout.Store({ route: url, id: 'select' });
    this.layoutMyfollow = new RouteLayout.Store({ route: `${url}/calendar`, id: 'myfollow' });
    this.detailLayout = new RouteLayout.Store({ route: `${url}`, id: 'detail' });
    this.selectPage = new SelectPage.Store({ route: `${url}/select` });
    this.rangeSelectPage = new SelectPage.Store({ route: `${url}/select` });
    this.typeSelectPage = new SelectPage.Store({ route: `${url}/select` });
    this.browser = new Browser.Store({ route: `${url}/select` });
    this.browserHrm = new BrowserHrm.Store({ route: `${url}/select` });
    this.browserHrm2 = new BrowserHrm.Store({ route: `${url}/select` });
  }

  getCalendarData = (params) => {
    this.ViewDatas = {
      conf: {},
      membersDatas: {},
      days: {},
    };
    let p = {
      bywhat: 2, // 日，周，月
      selectDate: this.selectedDate,
      objType: this.objType,
      objIds: this.objIds, // 
      workPlanType: this.workPlanType,
    };
    this.loadding = true;
    Api.getCalendarData({...p, ...params}).then(res => {
      this.loadding = false;
      if ( res && res.api_status ) {
        let membersDatas = {};
        Array.isArray(res.datas) && res.datas.map(v => { // 将数据处理为对象的形式
          membersDatas = {
            ...membersDatas,
            [v.memberid]: {
              ...v
            }
          };
        });
        
        this.ViewDatas = {
          ...this.ViewDatas,
          conf: { ...res.conf },
          membersDatas: membersDatas,
          days: res.days,
          memberids: res.memberids,
        };
      }
    });
  }

  // 一周日程安排日程数据
  getWorkPlanWeekData = (params) => {
    this.loadding = true;
    API.getWorkPlanWeekData(params).then(res => {
      this.loadding = false;
      this.init = true;
      this.oneWeekDatas = [];
      if(res.api_status && res) {
        this.showNight = res.showNight;
        this.showNoon = res.showNoon;
        this.weekStartDate = res.beginDate;
        this.weekEndDate = res.endDate;
        this.oneWeekDatas = res.datas;
      }
    });
  }

  
  // 获取日程一览表基本数据
  getBaseData = (params, callback) => {
    this.selectGroupVal = [];
    Api.getBaseData(params).then(res => {
      if (res) {
        this.selectGroup = res.selectGroup;
        this.selectedType = res.selectType;
        this.subComp = res.subComp;
        this.dept = res.dept;
        res.selectGroup && res.selectGroup[0] && this.selectGroupVal.push( res.selectGroup[0].key );
        this.selectedTypeLabel = res.selectGroup && res.selectGroup[0] && res.selectGroup[0].showname || '';
        this.titleFold = res.titleFold;
        if (res.selectGroup && res.selectGroup[0] && res.selectGroup[0].key) {
          let key = res.selectGroup[0].key;
          this.objType = key;
          if ( key === '1' ) {// 部门
            this.objIds = this.dept && this.dept[0] && this.dept[0].id || '';
            this.typeBrowserVal = this.dept;
          }
          if ( key === '2' ) {// 分部
            this.objIds = this.subComp && this.subComp[0] && this.subComp[0].id || '';
            this.typeBrowserVal = this.subComp;
          }
        }
        typeof callback === 'function' && callback();
      }
    });
  }

}

